const app = getApp()
import util from '../../../utils/util'
import {
  AddTalk
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 1,
    member: '',
    type: 2,
    content: '',
    src: "",
    video_cont: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let model = wx.getStorageSync('model')
    if (model) {
      this.setData({
        model
      })
    }
    this.setData({
      member: wx.getStorageSync('member')
    })
  },

  inputContent(e) {
    this.setData({
      content: e.detail.value
    })
  },

  /**
   * 选择视频
   */
  chooseVideo: function(e) {
    var _this = this;
    console.log('上传视频类型：',e.currentTarget.dataset.type);
    let sourceType = ['album','camera'];
    if(e.currentTarget.dataset.type==1){
     sourceType=['album'];
    }else{
      sourceType=['camera'];
    }

    wx.chooseVideo({
      sourceType: sourceType,
      success: function(res) {
        _this.setData({
          src: res.tempFilePath,
        })
        console.log(_this.data.src)
        _this.uploadvideo();
      }
    })
  },

  /**
   * 上传视频 目前后台限制最大100M, 以后如果视频太大可以选择视频的时候进行压缩
   */
  uploadvideo: function() {
    let that = this;
    var src = this.data.src;
    wx.uploadFile({
      url: app.globalData.baseURL + '/api/UploadsApi/upload',
      method: 'POST',
      filePath: src,
      name: 'file', // 服务器定义key字段名称
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function(r) {
        console.log('成功r:', r)
        let result = JSON.parse(r.data);
        console.log('视频地址：', result.data)
        let videoArr = [];
        videoArr.push(result.data)
        that.setData({
          video_cont: videoArr
        })
        console.log('视频上传成功')
      },
      fail: function() {
        console.log('接口调用失败')
      }
    })
  },

  /**
   * 点击删除视频
   */
  deleteVideo: function(e) {
    var _this = this;
    var src = _this.data.src;
    wx.showModal({
      title: '提示',
      content: '确认要删除该视频吗?',
      success: function(res) {
        if (res.confirm) {
          console.log("点击确定了")
          var unsrc = '';
          _this.setData({
            src: unsrc
          })
        } else if (res.cancel) {
          console.log("点击取消了");
          return false
        }
      }
    })
  },

  formSubmit() {
    let that = this;
    let content = this.data.content.replace(/(^\s*)|(\s*$)/g, "");
    if (!content && this.data.video_cont.length == 0) {
      if (!content) {
        wx.showModal({
          showCancel: false,
          content: '请填写内容',
        })
        return;
      }
      if (this.data.video_cont.length == 0) {
        wx.showModal({
          showCancel: false,
          content: '请上传视频',
        })
        return;
      }
    }

    let params = {
      username:that.data.member.name,
      idcard:that.data.member.idcard,
      type:that.data.type,
      vollege_id:that.data.member.vollege_id,
      town_id:that.data.member.town_id,
      content:that.data.content,
      video_cont:that.data.video_cont,
      headimg:that.data.heard_img,
      vollege_name:'',
      address:'',
      belong_vollege_id:app.globalData.vollege_id,
      belong_town_id:app.globalData.town_id,
    };

    util.apiRequest(AddTalk, params).then(r => {
      console.log('结果：', r)
      wx.requestSubscribeMessage({
        tmplIds: that.data.tmplIds,
        success(res) {
          console.log('成功', res)
        }
      })
      if (r.data.code == 1) {
        wx.showModal({
          showCancel: false,
          content: r.data.info,
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: "/pages/index/index"
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: r.data.info,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

})